package archimede;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;

public class PuntoCritico extends JTabbedPane {

	private JButton plus = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("plus.png")));
	private int count = 0;
	private StartingWindow st;
	
	protected void paintComponent(Graphics g) {
		setOpaque(false);
		g.drawImage(
				(new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg")))
						.getImage(), 0, 0, null);
		super.paintComponent(g);
	}
	
	public PuntoCritico(final StartingWindow st) {
		super();
		this.st = st;
		plus.setBorder(null);
		plus.setContentAreaFilled(false);
		plus.setRolloverEnabled(true);
		plus.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("plus_roll.png")));
		plus.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				addTab("P" + (++count)+"   ", new JScrollPane(new ObjP(st, count), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
						JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED){@Override
						public void setVerticalScrollBar(
								JScrollBar verticalScrollBar) {
							// TODO Auto-generated method stub
							verticalScrollBar.setUnitIncrement(15);
							super.setVerticalScrollBar(verticalScrollBar);
						}});
				setTabComponentAt(count, new ButtonTabObjP(PuntoCritico.this));
			}
		});
		addTab("", null);
		setTabComponentAt(count, plus);

	}

	public String print() {
		String print = "\n#" + count;
		for (int i = 1; i <= count; i++) {
			// TODO MODIFY HERE
			print = print + ((ObjP)((JViewport)(((JScrollPane)getComponent(i)).getComponent(0))).getView()).print();
		}
		// System.out.println(print);
		print = print + "\n@@Punto critico";
		return print;
	}

	public void setElement(String data) {
		Scanner c = new Scanner(data);
		c.nextLine();
		count = Integer.parseInt(c.nextLine().substring(1));
		for (int i = 1; i <= count; i++) {
			ObjP tmp = new ObjP(st, i);
			
			String value = c.nextLine();
			boolean next = true;
			while (c.hasNextLine() && next) {
				String line;
				line = c.nextLine();
				// String line = c.nextLine().substring(1);

				if (line.equalsIgnoreCase("#STOP")) {
					tmp.setElement(value);
					next = false;
				}
				value = value + "\n" + line;
			}
//			System.out.println("MMMMMMM : " + i + "\n" + value);
			addTab("P" + (i), new JScrollPane(tmp, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED){@Override
				public void setVerticalScrollBar(
						JScrollBar verticalScrollBar) {
					// TODO Auto-generated method stub
					verticalScrollBar.setUnitIncrement(15);
					super.setVerticalScrollBar(verticalScrollBar);
				}});
			setTabComponentAt(i, new ButtonTabObjP(PuntoCritico.this));

		}
		setSelectedIndex(count);
	}

	public Document pdf(Document doc) {
		doc.newPage();
		Chunk underline = new Chunk("9. Punti critici e soluzioni",
				FontFactory.getFont(FontFactory.TIMES, 18, Font.BOLD));
		underline.setUnderline(1.2f, -2f); // 0.1 thick, -2 y-location

		try {
			doc.add(underline);
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		for (int i = 1; i <= count; i++) {
			((ObjP)((JViewport)(((JScrollPane)getComponent(i)).getComponent(0))).getView()).pdf(doc, i);
			doc.newPage();
		}

		return doc;
	}

	public void setCount(int c) {
		count = c;
	}

	public int getCount() {
		return count;
	}
}
